home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Texteditors / Origami / bindings / fun / regions < prev    next >
Encoding:
Text File  |  1996-09-27  |  10.2 KB  |  414 lines

  1. @if-using not(ocl-file-regions)
  2.   @use (ocl-file-regions)
  3.   ;OCL{{{}}}
  4.   ;OCL{{{  libs
  5.   @if-using not(ocl-file-go-line) @lib go-line @fi
  6.   @if-using not(ocl-file-delchar) @lib delchar @fi
  7.   ;OCL}}}
  8.   ;OCL{{{  vars
  9.   ( defvar
  10.      ( start-line     ; screen position for start mark
  11.        start-x        ; start of defined block
  12.        start-y        ;           "
  13.        end-line       ; screen position for end mark
  14.        end-x          ; end of defined block
  15.        end-y          ;           "
  16.        folderr        ; return: could the folding be done?
  17.        end-splitted   ; return: was the last line of the block splitted?
  18.        start-splitted ; return: was the first line of the block splitted?
  19.        regions-help   ; internal help variable
  20.      )
  21.   )
  22.   ;OCL}}}
  23.   ;OCL{{{  set-start-mark
  24.   ( deffun set-start-mark
  25.      ( set start-y store-line
  26.        set start-x store-pos
  27.        set start-line cursor-level
  28.      )
  29.   )
  30.   ;OCL}}}
  31.   ;OCL{{{  go-start-mark
  32.   ( deffun go-start-mark
  33.      ( set go-line-arg start-y
  34.        go-line
  35.        goto-counter start-x
  36.        redraw-display
  37.      )
  38.   )
  39.   ;OCL}}}
  40.   ;OCL{{{  set-end-mark
  41.   ( deffun set-end-mark
  42.      ( set end-y store-line
  43.        set end-x store-pos
  44.        set end-line cursor-level
  45.      )
  46.   )
  47.   ;OCL}}}
  48.   ;OCL{{{  clear-marks
  49.   ( deffun clear-marks
  50.      ( set start-x 0
  51.        set start-y 0
  52.        set end-x 0
  53.        set end-y 0
  54.      )
  55.   )
  56.   ;OCL}}}
  57.   ;OCL{{{  change-mark-counters
  58.   ( defvar ( dummy ) )
  59.   ( deffun change-mark-counters
  60.      (
  61.        ;OCL{{{  change x
  62.        set dummy end-x
  63.        set end-x start-x
  64.        set start-x dummy
  65.        ;OCL}}}
  66.        ;OCL{{{  change y
  67.        set dummy end-y
  68.        set end-y start-y
  69.        set start-y dummy
  70.        ;OCL}}}
  71.        ;OCL{{{  change level
  72.        set dummy end-line
  73.        set end-line start-line
  74.        set start-line dummy
  75.        ;OCL}}}
  76.      )
  77.   )
  78.   ( undeclare ( dummy ) )
  79.   ;OCL}}}
  80.   ;OCL{{{  fold-region
  81.   ;OCL{{{  vars
  82.   ( defvar
  83.      ( entered        ; help, number of minimum leading spaces in given block
  84.        bool           ; help
  85.        y              ; help
  86.        cy             ; help
  87.      )
  88.   )
  89.   ;OCL}}}
  90.   ;OCL{{{  mini-x
  91.   ( deffun mini-x
  92.      ( goto-counter 1
  93.        while true
  94.         ( case
  95.            ( test-end-line ( return-from-macro ) )
  96.            ( test-char "  ( forward-character ) )
  97.           default
  98.            ( if >(cy store-pos)
  99.               ( set cy store-pos )
  100.              fi
  101.              return-from-macro
  102.            )
  103.           esac
  104.         )
  105.      )
  106.   )
  107.   ;OCL}}}
  108.   (deffun fold-region
  109.     ( set folderr 1
  110.       ;OCL{{{  start-y = start, end-y = end, cy=start, y=0, entered=0
  111.       set entered 0
  112.       ;OCL{{{  maybe change end/start-positions
  113.       case
  114.        ;OCL{{{  start under end -> change
  115.        ( pre
  116.           ( set y -(start-y end-y) )
  117.             >(y 0)
  118.          ( change-mark-counters )
  119.        )
  120.        ;OCL}}}
  121.        ;OCL{{{  one line, start after end
  122.        ( and ( =(y 0)
  123.                pre (set y -(start-x end-x) ) >(y 0)
  124.              )
  125.          ( change-mark-counters )
  126.        )
  127.        ;OCL}}}
  128.       esac
  129.       ;OCL}}}
  130.       ;OCL{{{  correct marks given?
  131.       if not(and(start-x start-y end-x end-y)) ( return-from-macro ) fi
  132.       ;OCL}}}
  133.       set y 0
  134.       set cy start-y
  135.       ;OCL}}}
  136.       screen-off
  137.       ;OCL{{{  go from start to end and count steps
  138.       ;OCL{{{  go-line(start-y)
  139.       set go-line-arg start-y
  140.       go-line
  141.       if test-begin-fold ( close-fold ) fi
  142.       ;OCL}}}
  143.       while pre ( set bool -(cy end-y) ) <>(bool 0)
  144.        ;OCL{{{  move one step and update counters
  145.        ( if >(bool 0)
  146.           ;OCL{{{  under end mark
  147.           ( set y -(y 1)
  148.             previous-line
  149.             if test-fold-line
  150.              ;OCL{{{  open fold to search in this fold
  151.              ( open-fold )
  152.              ;OCL}}}
  153.             else
  154.              ;OCL{{{  cannot fold
  155.              ( screen-on
  156.                redraw-display
  157.                return-from-macro
  158.              )
  159.              ;OCL}}}
  160.             fi
  161.           )
  162.           ;OCL}}}
  163.          else
  164.           ;OCL{{{  handle open/close fold
  165.           ( case
  166.              ;OCL{{{  handle fold-open
  167.              ( test-begin-fold ( set entered +(entered 1) ) )
  168.              ;OCL}}}
  169.              ;OCL{{{  handle fold-end
  170.              ( test-end-fold
  171.                 ( if =(entered 0)
  172.                    ;OCL{{{  cannot fold
  173.                    ( screen-on
  174.                      redraw-display
  175.                      return-from-macro
  176.                    )
  177.                    ;OCL}}}
  178.                   fi
  179.                   set entered +(entered -1)
  180.                 )
  181.              )
  182.              ;OCL}}}
  183.             esac
  184.             set y +(y 1)
  185.             next-line
  186.           )
  187.           ;OCL}}}
  188.          fi
  189.          set cy store-line
  190.        )
  191.        ;OCL}}}
  192.       if or
  193.           ( and(    test-end-fold  <>(1 entered))
  194.             and(not(test-end-fold) <>(entered 0))
  195.             test-begin-fold
  196.           )
  197.        ;OCL{{{  cannot fold
  198.        ( screen-on
  199.          redraw-display
  200.          return-from-macro
  201.        )
  202.        ;OCL}}}
  203.       fi
  204.       ;OCL}}}
  205.       ;OCL{{{  maybe splitt last line of the marked region
  206.       goto-counter end-x
  207.       forward-character
  208.       if or(not(test-text),test-end-line,pre ( "$ ) last-message M_ERR_PO)
  209.        ;OCL{{{  no splitt
  210.        ( set end-splitted 0 )
  211.        ;OCL}}}
  212.       else
  213.        ;OCL{{{  do the splitt
  214.        ( set end-splitted 1
  215.          if test-end-line ( end-of-line ) fi
  216.          backward-character
  217.          newline-and-indent
  218.          newline-and-indent
  219.          previous-line
  220.          beginning-of-line
  221.          do
  222.           ( delete-character )
  223.          while not(test-char "$)
  224.          delete-character
  225.          previous-line
  226.        )
  227.        ;OCL}}}
  228.       fi
  229.       ;OCL}}}
  230.       ;OCL{{{  move back to start, set cy to minimum x-position, entered becomes invalid
  231.       goto-counter end-x
  232.       set cy store-pos
  233.       mini-x
  234.       local (y)
  235.        ( while <>(y 0)
  236.           ( previous-line
  237.             set y -(y 1)
  238.             if >(cy 1)
  239.              ( mini-x )
  240.             fi
  241.           )
  242.        )
  243.       ;OCL}}}
  244.       ;OCL{{{  maybe splitt first line of the marked region
  245.       if
  246.          and
  247.           ( test-text,
  248.             pre
  249.              ( beginning-of-line
  250.                set start-splitted -(start-x store-pos)
  251.                goto-counter start-x
  252.                if test-end-line ( end-of-line ) fi
  253.              )
  254.             >(start-splitted 0)
  255.           )
  256.        ;OCL{{{  do the splitt
  257.        ( set start-splitted 1
  258.          "$
  259.          backward-character
  260.          newline-and-indent
  261.          newline-and-indent
  262.          previous-line
  263.          beginning-of-line
  264.          do
  265.           ( delete-character )
  266.          while not(test-char "$)
  267.          delete-character
  268.          mini-x
  269.        )
  270.        ;OCL}}}
  271.       else
  272.        ;OCL{{{  no splitt
  273.        ( set start-splitted 0 )
  274.        ;OCL}}}
  275.       fi
  276.       ;OCL}}}
  277.       ;OCL{{{  do the fold
  278.       goto-counter cy
  279.       delete-mode-view
  280.       create-fold
  281.       while >(y 0) ( next-line set y +(y -1))
  282.       next-line
  283.       create-fold
  284.       ;OCL}}}
  285.       screen-on
  286.       redraw-display
  287.       set folderr counter test-folding
  288.     )
  289.   )
  290.   ;OCL{{{  undeclare
  291.   ( undeclare ( mini-x entered bool y cy ) )
  292.   ;OCL}}}
  293.   ;OCL}}}
  294.   ;OCL{{{  combine-splitted-parts-start
  295.   ( deffun combine-splitted-parts-start
  296.      (
  297.        ;OCL{{{  go-line start-y
  298.        set go-line-arg start-y
  299.        go-line
  300.        ;OCL}}}
  301.        ;OCL{{{  generate $ at real start of line
  302.        end-of-line
  303.        newline-and-indent
  304.        beginning-of-line
  305.        next-line
  306.        "$
  307.        ;OCL}}}
  308.        ;OCL{{{  move to start-x/y
  309.        previous-line
  310.        previous-line
  311.        goto-counter start-x
  312.        "$
  313.        ;OCL}}}
  314.        ;OCL{{{  remove to $
  315.        do
  316.         ( delete-character )
  317.        while test-char "   ;
  318.        ;OCL}}}
  319.        delete-character
  320.        delete-previous-character
  321.      )
  322.   )
  323.   ;OCL}}}
  324.   ;OCL{{{  combine-splitted-parts-end
  325.   ( deffun combine-splitted-parts-end
  326.      (
  327.        ;OCL{{{  go-line end-y
  328.        set go-line-arg end-y
  329.        go-line
  330.        ;OCL}}}
  331.        if and(test-text,pre ( next-line ) test-text )
  332.         ( previous-line
  333.           ;OCL{{{  generate $ at real start of line
  334.           end-of-line
  335.           newline-and-indent
  336.           beginning-of-line
  337.           next-line
  338.           "$
  339.           ;OCL}}}
  340.           ;OCL{{{  move to end-x/y
  341.           previous-line
  342.           previous-line
  343.           goto-counter end-x
  344.           forward-character
  345.           "$
  346.           ;OCL}}}
  347.           ;OCL{{{  remove to $
  348.           do
  349.            ( delete-character )
  350.           while test-char "   ;
  351.           ;OCL}}}
  352.           delete-character
  353.           delete-previous-character
  354.         )
  355.        else
  356.         ( if test-end-fold (close-fold) fi )
  357.        fi
  358.      )
  359.   )
  360.   ;OCL}}}
  361.   ( demand-load (
  362.      ;OCL{{{  indent-region
  363.      @if-using (REGIONS-INDENT)
  364.         ;OCL{{{  comment
  365.         ; try to indent a region +x spaces
  366.         ; x-positions are set to beginning of first and end of last line
  367.         ; x=0: the marked lines are folded. Start- and end-line are NOT splitted and
  368.         ; are moved complete in the fold!
  369.         ;OCL}}}
  370.         ( deffun ( width ) indent-region
  371.            ( screen-off
  372.              ;OCL{{{  fold indent stuff
  373.              if =(start-y end-y)
  374.               ( set go-line-arg start-y
  375.                 go-line
  376.                 if or(test-top test-view)
  377.                  ( set folderr 1 )
  378.                 else
  379.                  ( beginning-of-line
  380.                    create-fold
  381.                    next-line
  382.                    create-fold
  383.                    set folderr 0
  384.                  )
  385.                 fi
  386.               )
  387.              else
  388.               ( if >(-(start-y end-y) 0) ( change-mark-counters ) fi
  389.                 set end-x 32000
  390.                 set start-x 1
  391.                 fold-region
  392.               )
  393.              fi
  394.              ;OCL}}}
  395.              if =(folderr 0)
  396.               ;OCL{{{  shift and unfold
  397.               ( beginning-of-line
  398.                 while >(width 0)
  399.                  ( "  set width +(width -1) )
  400.                 while <>(width 0)
  401.                  ( delete-previous-character set width +(width 1) )
  402.                 unfold-fold
  403.               )
  404.               ;OCL}}}
  405.              fi
  406.              screen-on
  407.              set-cursor-line start-line
  408.            )
  409.         )
  410.      @fi
  411.      ;OCL}}}
  412.   ) )
  413. @fi
  414.